<template>
  <div>
    <el-col :span="12" style="margin-bottom:15px"
      ><el-button type="primary" @click="addClk">新增</el-button>
      <el-button type="success" @click="batchRemoveClk"
        >批量删除</el-button
      ></el-col
    >
    <el-col :span="12">
      <el-input
        placeholder="请输入内容"
        v-model="input"
        class="input-with-select"
      >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="doSearch"
        ></el-button>
      </el-input>
    </el-col>
    <el-table
      @selection-change="handleSelectionChange"
      :data="tableData"
      border
      style="width: 100%"
    >
      <el-table-column fixed type="selection" width="55"> </el-table-column>
      <el-table-column
        fixed
        type="index"
        width="50"
        label="#"
      ></el-table-column>
      <el-table-column prop="title" label="标题" width="200"> </el-table-column>
      <el-table-column prop="content" label="内容" width="400">
      </el-table-column>
      <el-table-column prop="author" label="作者" width="50"> </el-table-column>
      <el-table-column prop="date" label="日期" width="150"> </el-table-column>
      <el-table-column prop="type" label="类型" width="150"> </el-table-column>
      <el-table-column prop="time" label="时间" width="80"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="150">
        <template slot-scope="scope">
          <el-button @click="updateClk(scope.row)" type="text" size="small"
            >修改</el-button
          >
          <el-button type="text" size="small" @click="removeClk(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-drawer
      :title="`${form.id ? '修改' : '新增'}用户`"
      :visible.sync="dialog"
      direction="rtl"
      custom-class="demo-drawer"
      ref="drawer"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="标题">
          <el-input v-model="form.title"></el-input>
        </el-form-item>

        <el-form-item label="作者">
          <el-input v-model="form.author"></el-input>
        </el-form-item>

        <el-form-item label="类别">
          <el-select v-model="form.type" placeholder="请选择类别">
            <el-option
              v-for="(item, index) in classifyList"
              :key="index"
              :label="item.name"
              :value="item.name"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="时间">
          <el-select v-model="form.time" placeholder="请选择事件">
            <el-option label="1" value="1"></el-option>
            <el-option label="3" value="3"></el-option>
            <el-option label="5" value="5"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="内容">
          <mavonEditor
            style="max-height: 300px; overflow-y: scroll"
            v-model="form.content"
          ></mavonEditor>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="doSubmit">立即创建</el-button>
          <el-button @click="cancelForm">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[2, 3, 5, 10]"
      :page-size="pageSize"
      layout="prev, pager, next"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import { mavonEditor } from "mavon-editor";
import "mavon-editor/dist/css/index.css";
import { mapState, mapMutations, mapActions } from "vuex";
export default {
  layout: "main",
  data() {
    return {
      input: "",
      dialog: false,
      form: {},
      formLabelWidth: "80px",
      deleteIds: "",
    };
  },
  mounted() {
    this.getData();
  },
  methods: {
    ...mapActions("msbd", ["getData", "addList", "updateList", "deleteList"]),
    ...mapMutations("msbd", ["CURRENTPAGE", "PAGESIZE"]),
    // 当前页改变
    async handleCurrentChange(val) {
      await this.CURRENTPAGE(val);
      if (this.input) {
        await this.getData(this.input);
      } else {
        await this.getData();
      }
    },

    // 页数改变
    async handleSizeChange(val) {
      await this.PAGESIZE(val);
      if (this.input) {
        await this.getData(this.input);
      } else {
        await this.getData();
      }
    },

    // 搜索
    async doSearch() {
      await this.getData(this.input);
      this.CURRENTPAGE(0);
    },

    // 点击全选
    handleSelectionChange(val) {
      this.ids = val
        .map((item) => {
          return item.id;
        })
        .join(",");
    },

    // 新增按钮
    addClk() {
      this.dialog = true;
    },

    // 修改
    updateClk(row) {
      this.form = { ...row };
      this.dialog = true;
    },

    // 删除
    async removeClk(id) {
      await this.deleteList({ id });
    },

    //批量删除
    async batchRemoveClk() {
      await this.deleteList({ id: this.ids });
    },

    // 提交表单
    async doSubmit() {
      if (this.form.id) {
        await this.updateList(this.form);
      } else {
        await this.addList(this.form);
      }
      this.dialog = false;
      this.clearForm();
    },

    // 取消提交
    cancelForm() {
      this.dialog = false;
      this.clearForm();
    },

    // 清空form
    clearForm() {
      this.form = {
        avatar: "",
      };
      this.imageUrl = "";
    },
  },
  computed: {
    ...mapState("msbd", [
      "tableData",
      "total",
      "currentPage",
      "pageSize",
      "classifyList",
    ]),
  },

  components: {
    mavonEditor,
  },
};
</script>

<style scoped>
</style>